package com.heatup.apt.common.util.Netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.util.CharsetUtil;

public class ByteFrameHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private final ChannelGroup group;

    public ByteFrameHandler(ChannelGroup group) {
        this.group = group;
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

        cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) throws Exception {
        System.out.println(in.toString(CharsetUtil.UTF_8));

        group.writeAndFlush(in.retain());

    }
}
